package ro.ubbcluj.cs.damate.services;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ro.ubbcluj.cs.damate.persistence.entities.Event;
import ro.ubbcluj.cs.damate.persistence.entities.File;
import ro.ubbcluj.cs.damate.persistence.entities.Project;
import ro.ubbcluj.cs.damate.persistence.entities.User;
import ro.ubbcluj.cs.damate.persistence.entities.Workspace;
import ro.ubbcluj.cs.damate.persistence.repository.FileRepository;
import ro.ubbcluj.cs.damate.persistence.repository.ProjectRepository;
import ro.ubbcluj.cs.damate.persistence.repository.WorkspaceRepository;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2013.06.28.
 *         Time: 15:44
 */
@Service
public class ObservedObjectService {

    @Autowired
    private WorkspaceRepository workspaceRepository;

    @Autowired
    private ProjectRepository projectRepository;

    @Autowired
    private FileRepository fileRepository;

    @Transactional
    public void saveWorkspace(Workspace transientWorkspace) {
        workspaceRepository.save(transientWorkspace);
    }

    @Transactional
    public void deleteWorkspace(Workspace persistentWorkspace) {
        workspaceRepository.delete(persistentWorkspace);
    }

    public Workspace findWorkspace(Event event, String ip, String path){
        List<Workspace> workspace = workspaceRepository.findByIpAddressAndPath(ip, path);
        if (workspace != null && !workspace.isEmpty()) {
            return workspace.get(0);
        } else {
            Workspace newWorkspace = new Workspace();
            newWorkspace.setIpAddress(ip);
            newWorkspace.setPath(path);
            newWorkspace.setOwner(event.getRelatedUser());
            newWorkspace.setCreationDate(event.getDate());
            return newWorkspace;
        }
    }

    public Workspace loadWorkspace(Workspace workspace) {
        return workspaceRepository.findOne(workspace.getObservedEntityId());
    }

    public List<Workspace> findWorkspaceByUserAndIP(User user, String ip){
        return workspaceRepository.findByUserAndIp(user, ip);
    }

    @Transactional
    public void saveProject(Project transientProject) {
        projectRepository.save(transientProject);
    }

    @Transactional
    public void deleteProject(Project persistentProject) {
        projectRepository.delete(persistentProject);
    }

    public Project findProject(Event event, String projectName, Workspace workspace) {
        Project project = projectRepository.findProject(projectName, workspace.getIpAddress(), workspace.getPath());
        if (project == null) {
            project = new Project();
            project.setName(projectName);
            project.setOwner(event.getRelatedUser());
            project.setCreationDate(event.getDate());
            List<Workspace> workspaces = new ArrayList<Workspace>();
            workspaces.add(workspace);
            project.setWorkspaces(workspaces);
            if (workspace.getProjects() == null) {
                workspace.setProjects(new ArrayList<Project>());
            }
            workspace.getProjects().add(project);
        }
        return project;
    }

    @Transactional
    public void saveFile(File transientFile) {
        fileRepository.save(transientFile);
    }

    @Transactional
    public void deleteFile(File persistentFile) {
        fileRepository.delete(persistentFile);
    }

    public File findFile(Event event, String filePath, int type, Project project, Workspace workspace) {
        File file = fileRepository.findFile(filePath, type, project.getName(), workspace.getIpAddress(), workspace.getPath());
        if (file == null) {
            file = new File();
            file.setPath(filePath);
            file.setType(type);
            String[] path = filePath.split("[\\/]+");
            file.setName(path[path.length-1]);
            file.setOwner(event.getRelatedUser());
            file.setCreationDate(event.getDate());
            file.setProject(project);
            if (project.getFiles() == null) {
                project.setFiles(new ArrayList<File>());
            }
            project.getFiles().add(file);
        }
        return file;
    }
}
